<?

// Establish domain prefix
$domainname=explode('.',$domaindata['domain']);
$domainprefix=$domainname[0];

// If action is set
if ($action == 'update') {
	unset($tmp);
	unset($error);
	unset($result);
	if (is_array($uid)) { // multiple user update.
		reset($uid);
		do {
			$tmpuid = trim(current($uid));
			$tmp = $data[$tmpuid];
			$tmp['uid'] = $tmpuid;
			$tmp['id'] = $domaindata['id'];
			if ($cfg['prepend_domain_to_username']) {
				// HARDCODED $data['username'] to include domainprefix
				$tmp['username']=$domainprefix."-".$tmp['username'];
			}
			$tmperr = user('update',$tmp);
			
			// Trap errors and 'add' them up.
			if ($tmperr) {
				reset($tmperr);
				foreach($tmperr AS $key=>$val)
					$tmperr[$key] = "$tmpuid: $val";
				
				if ($error) $error = array_merge($error,$tmperr);
				else $error = $tmperr;
			}
			else
				$result .= "$tmpuid . ";
		} while(next($uid));
	}
	else $error[1] = $T['err']['nothing to update'];
	if ($error)
		echo "<br>&nbsp;&nbsp;� ".implode("<br>\n&nbsp;&nbsp;� ",$error)."<br>\n";
	if ($result) 
		echo $T['user '.$action]."<b>".$result."</b><br>\n";
}
elseif ($action AND $confirm) {
	// check if its a GET or POST (i.e is $uid a string or an array?)
	if (!is_array($uid)) { // single action from GET
		// hardcode data
		$data['username'] = trim($uid);
		$data['id'] = $domaindata['id'];
		$error = user($action,$data);
	}
	elseif (is_array($uid)) { // multiple user action.
		// hardcode data
		$data['id'] = $domaindata['id'];
		
		unset($error);
		reset($uid);
		do {
			$data['username'] = trim(current($uid));
			$tmperr = user($action,$data);
			
			// Trap errors and 'add' them up.
			if ($tmperr) {
				if ($error) $error = array_merge($error,$tmperr);
				else $error = $tmperr;
			}
		} while(next($uid));
		$uid = implode(", ",$uid);
	}
	else $error[1] = $T['Invalid Input'];

	if ($error)
		echo "<br>&nbsp;&nbsp;� ".implode("<br>\n&nbsp;&nbsp;� ",$error)."<br>\n";
	else 
		echo $T['user '.$action]."<b>".$uid."</b><br>\n";
}
// ask confirmation for suspend - unsuspend - remove
elseif ($action AND $uid AND !$confirm) {
	if (is_array($uid)) $tmp = implode(", ",$uid);
	else $tmp = $uid;
?>
<blockquote>
<form action="<?=$current_url;?>" method="post" name="webcp1" onSubmit="submitonce(this);">

&nbsp;&nbsp;� <?=$T['really '.$action];?><b><?=$tmp;?></b><br>
&nbsp;&nbsp;&nbsp;<input type="submit" value="<?=$T['yes'];?>">
<? if (is_array($uid)) {
	$i = 0;
	do {
		echo '<input type="hidden" name="uid['.$i++.']" value="'.current($uid).'">';
		echo "\n";
	} while(next($uid));
}
else echo '<input type="hidden" name="uid" value="'.$uid.'">';
?>
<input type="hidden" name="action" value="<?=$action;?>">
<input type="hidden" name="confirm" value="true">
</form>
</blockquote>
<? }

$sql_query = 'SELECT suspend, action, username, name, id, root, type, level, hd_used, hd, aliases, catchall, forward';

// If $search, do it; create appropriate SQL query.
if ($data['dosearch'] == 'true') {
	// remove spaces
	foreach($data AS $key=>$val)
		$data[$key] = trim($val);
	
	// prepare sql command
	$sql_query .= " FROM users WHERE id=".$domaindata['id']." AND action != 'remove' ";
	if ($data['searchin'] != 'all')
		$sql_query .= "AND ".$data['searchin']." LIKE '%".$data['search']."%' ";
	else {
		$sql_query .= "AND (username LIKE '%".$data['search']."%' OR name LIKE '%".
				$data['search']."%' OR aliases LIKE '%".$data['search'].
				"%' OR forward LIKE '%".$data['search']."%') ";
	}
	if ($data['order'] == 'username' OR $data['order'] == 'name')
		$sql_query .= "ORDER BY ".$data['order'].",level,username";
	elseif ($data['order'] == 'hdusage')
		$sql_query .= "ORDER BY hd_used/hd DESC,level,username";
	else
		$sql_query .= "ORDER BY level,username";
}
else 	// Else get user list
	$sql_query .= " FROM users WHERE id=".$domaindata['id']." AND action != 'remove' ORDER BY level,username";

$dbp = mysql_query($sql_query);

// check for users, else display message
$numrows = mysql_num_rows($dbp);

if (!$numrows AND $data['dosearch'] != 'true') {
	$s[] = '$number';
	$r[] = $domaindata['id'];
	$s[] = '$user';
	$r[] = $user;
	echo str_replace($s, $r, $T['no users']);
} else {
	//if 1 or more users, display search 
	if ($numrows > 0 OR $data['dosearch'] == 'true') {
		unset($chk_in);
		unset($chk_show);
		unset($chk_order);
		$chk_in[$data['searchin']] = ' selected';
		$chk_show[$data['show']] = ' selected';
		$chk_order[$data['order']] = ' selected';
		?>

<div align="center">
<form action="<?=$current_url;?>" method="post" name="webcp2" onSubmit="submitonce(this);">
<input type="hidden" name="data[dosearch]" value="true">
<table border="0" cellpadding="0" cellspacing="0" summary="">
	<tr>
		<td class="tblbg">
<table border="0" cellpadding="0" cellspacing="1" summary="">
	<tr>
		<td class="row1">
<table border="0" cellspacing="1" summary="">
	<tr>
		<td class="small"><?=$T['Search'];?></td>
		<td><input type="text" name="data[search]" size="15" maxlength="256" value="<?=$data['search'];?>" class="small"></td>
		<td class="small"><?=$T['in'];?></td>
		<td><select name="data[searchin]" class="small">
	      	<option value="all"<?=$chk_in['all'];?>> <?=$T['All'];?></option>
	      	<option value="username"<?=$chk_in['username'];?>> <?=$T['Username'];?></option>
	      	<option value="name"<?=$chk_in['name'];?>> <?=$T['Full Name'];?></option>
	      	<option value="aliases"<?=$chk_in['aliases'];?>> <?=$T['E-Mail Aliases'];?></option>
	      	<option value="forward"<?=$chk_in['forward'];?>> <?=$T['E-Mail Forward'];?></option>
	      </select></td>
	        <td class="small"><?=$T['order by'];?></td>
		<td><select name="data[order]" class="small">
	      	<option value="default"<?=$chk_order['default'];?>> <?=$T['default'];?></option>
	      	<option value="username"<?=$chk_order['username'];?>> <?=$T['Username'];?></option>
	      	<option value="name"<?=$chk_order['name'];?>> <?=$T['Full Name'];?></option>
	      	<option value="hdusage"<?=$chk_order['hdusage'];?>> <?=$T['Hard Disk Usage'];?></option>
	      </select></td>
		<td class="small"><?=$T['display'];?></td>
		<td><select name="data[show]" class="small">
	      	<option value="brief"<?=$chk_show['brief'];?>> <?=$T['Brief'];?></option>
	      	<option value="detailed"<?=$chk_show['detailed'];?>> <?=$T['Detailed'];?></option>
	      	<option value="editable"<?=$chk_show['editable'];?>> <?=$T['Editable'];?></option>
	      </select></td>
		<td><input type="submit" value="<?=$T['Search'];?>" class="small"></td>
	</tr>
</table></td>
	</tr>
</table></td>
	</tr>
</table>
<?
if (!$numrows AND $data['dosearch'] == 'true')
	echo '<br>'.$T['no results'];
?>
</div>
</form><br>
	<? } ?>

<div align="center">
<form action="<?=$current_url;?>" method="post" name="webcp3" onSubmit="submitonce(this);">
<table border="0" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellspacing="1">
<?
// start displaying users

$i = 0;
while ($userlist = mysql_fetch_array($dbp)) {
	foreach($userlist AS $key=>$val)
		$userlist[$key] = trim($val);
	
	// select cell bg color
	($i++ % 2) ? ($cell = "row1") : ($cell = "row2");
	
	// if the current user is suspended, put no bg color
	if (($userlist['suspend'] == "true" OR $userlist['action'] == "suspend") AND $userlist['action'] != "unsuspend")
		$cell = "suspend";
	?>
	<tr>
		<? if ($numrows > 0) { ?><td class="<?=$cell;?>" width="10">
		<input type="checkbox" id="uid<?=$i;?>" name="uid[]" value="<?=$userlist['username'];?>" <?if ($userdata['level'] > $userlist['level']) {?>disabled<? } ?>>
		</td><? } ?>
		<td class="<?=$cell;?>" width="500">
		<b>
		<? if ($userdata['level'] <= $userlist['level']) { ?><a href="./?cp=personal&url=userset&number=<?=$number;?>&user=<?=$userlist['username'];?>" target='_top'><? } ?><?=$userlist['username'];?><? if ($userdata['level'] <= $userlist['level']) { ?></a><? } ?>
		</b>
		&nbsp;
		(<?=$userlist['name'];?>)
		&nbsp;
		<? if ($data['show'] == 'detailed') { ?>
		<br><div class="small">
		<?
		$SQL = "SELECT time, log_msg, extra, remote_ip FROM log WHERE log_msg like 'login %' AND extra = '".$userlist['username']."' ORDER BY time desc LIMIT 0,1";
		$res = mysql_query($SQL);
		if ($row = mysql_fetch_array($res)) {
			echo $T['Latest Login'].":&nbsp;&nbsp;<i>".str_replace("login ", "", $row["log_msg"])." - ".date("n/j g:ia", strtotime($row["time"]))."</i><br>\n";
		}
		$root = trim(str_replace($cfg['webdir']."/".$userlist['id'],"",$userlist['root']));
		if (!$root)
			$root = "/";
		echo $T['User Root Directory'].":&nbsp;&nbsp;<i>$root</i><br>\n";
		if ($userlist['type'] == 'demo')
			$tmp = $T['usertype']['demo'].' - ';
		else
			unset($tmp);
		echo $T['Access Level'].":&nbsp;&nbsp;<i>$tmp".$T['userlvl'][$userlist['level']]."</i><br>\n";
		echo $T['Hard Disk Usage'].":&nbsp;&nbsp;<i>".$userlist['hd_used']." / ".(($userlist['hd'] != -1) ? $userlist['hd']." ".$T['mb']." (".round($userlist['hd_used'] / $userlist['hd'] * 100,2)."%)" : $T['Unlimited'])."</i><br>\n";
		if (trim($userlist['aliases'])) {
			echo $T['E-Mail Aliases'].":&nbsp;&nbsp;<i>";
			if ($userlist['catchall'] == 'true')
				echo "*".$T['Catch-All']."*, ";
			echo str_replace(" ",", ",trim($userlist['aliases']))."</i><br>\n";
		}
		elseif ($userlist['catchall'] == 'true')
			echo $T['Catch-All'].":&nbsp;&nbsp;<i>".$T['true']."</i><br>\n";
		if (trim($userlist['forward']))
			echo $T['E-Mail Forward'].":&nbsp;&nbsp;<i>".str_replace(" ",", ",trim($userlist['forward']))."<br></i>\n";
		?>
		</div>		
		<? } elseif ($data['show'] == 'editable' && $userdata['level'] <= $userlist['level']) { ?>
		<br><div class="small">
		<table border="0" cellpadding="0" cellspacing="1">
		<tr>
		<td class="small">&nbsp;<?=$T['Username'];?>&nbsp;</td>
		<td class="small">&nbsp;<?=$T['Full Name'];?>&nbsp;</td>
		<td class="small">&nbsp;<?=$T['Access Level'];?>&nbsp;</td>
		</tr>
		<tr>
		<td class="small">&nbsp;<?=(($cfg['prepend_domain_to_username']) ? $domainprefix."-" : "");?><input onchange="getElementById('uid<?=$i;?>').checked = true; action.value = 'update';" class="small" type="text" name="data[<?=$userlist['username'];?>][username]" size="10" maxlength="255" value="<?=(($cfg['prepend_domain_to_username']) ? str_replace($domainprefix."-", "", $userlist['username']) : $userlist['username']);?>">&nbsp;</td>
		<td class="small">&nbsp;<input onchange="getElementById('uid<?=$i;?>').checked = true; action.value = 'update';" class="small" type="text" name="data[<?=$userlist['username'];?>][name]" size="30" maxlength="255" value="<?=$userlist['name'];?>">&nbsp;</td>
		<td class="small">&nbsp;<select onchange="getElementById('uid<?=$i;?>').checked = true; action.value = 'update';" class="small" name="data[<?=$userlist['username'];?>][level]">
		<? for ($j=4; $j>=$userdata['level'];$j--) {
			echo '<option value="'.$j.'"';
			if ($userlist['level'] == $j) echo " SELECTED";
			echo '> '.$T['userlvl'][$j].'</option>';
			echo "\n";
		} ?>
		</select>&nbsp;</td>
		</tr>
		</table>
		<table border="0" cellpadding="0" cellspacing="1">
		<tr>
		<td class="small">&nbsp;<?=$T['E-Mail Aliases'];?>&nbsp;</td>
		<td class="small">&nbsp;<?=$T['E-Mail Forward'];?>&nbsp;</td>
		</tr>
		<tr>
		<td class="small">&nbsp;<textarea onchange="getElementById('uid<?=$i;?>').checked = true; action.value = 'update';" class="small" name="data[<?=$userlist['username'];?>][aliases]" cols="30" rows="2"><?=str_replace(" ",", ",$userlist['aliases']);?></textarea>&nbsp;</td>
		<td class="small">&nbsp;<textarea onchange="getElementById('uid<?=$i;?>').checked = true; action.value = 'update';" class="small" name="data[<?=$userlist['username'];?>][forward]" cols="30" rows="2"><?=str_replace(" ",", ",$userlist['forward']);?></textarea>&nbsp;</td>
		</tr>
		</table>
		</div>
		<? } else { ?>
		<font class="small"><? if ($userlist['type'] == 'demo') { echo $T['usertype']['demo'].' - '; } echo $T['userlvl'][$userlist['level']];?></font>
		<? } ?></td>
		<td class="<?=$cell;?>" width="50"><nobr>&nbsp;
		<? // select suspend or unsuspend icon/link
		if ($userdata['level'] <= $userlist['level']) {
		if ($cell == 'suspend') { ?>
		<a href="<?=$current_url;?>&action=unsuspend&confirm=true&uid=<?=$userlist['username'];?>"><img src="icon/unsuspend.gif" border="0" width="16" height="16" alt="Unsuspend"></a><? } else { ?>
		<a href="<?=$current_url;?>&action=suspend&uid=<?=$userlist['username'];?>"><img src="icon/suspend.gif" border="0" width="16" height="16" alt="Suspend"></a><? } ?>
		<a href="<?=$current_url;?>&action=remove&uid=<?=$userlist['username'];?>"><img src="icon/remove.gif" border="0" width="15" height="15" alt="Remove"></a></nobr>
		<? } ?>
		</td>
	</tr>
	<? 
	}
	?>
</table><br><?
if ($numrows > 0) {
	if ($data['dosearch'] == "true") { ?>
	<input type="hidden" name="data[dosearch]" value="true">
	<input type="hidden" name="data[search]" value="<?=$data['search'];?>">
	<input type="hidden" name="data[searchin]" value="<?=$data['searchin'];?>">
	<input type="hidden" name="data[order]" value="<?=$data['order'];?>">
	<input type="hidden" name="data[show]" value="<?=$data['show'];?>">
	<? } ?>

<div align="right">
<select name="action" class="small">
	<option value=""> <?=$T['Select Action'];?></option>
	<? if ($data['show'] == 'editable') { ?><option value="update"> <?=$T['Modify Users'];?></option><? } ?>
	<option value="suspend"> <?=$T['Suspend Users'];?></option>
	<option value="unsuspend"> <?=$T['Reactivate Users'];?></option>
	<option value="remove"> <?=$T['Remove Users'];?></option>
</select>
&nbsp;
<input  class="small" type="submit" value="<?=$T['Apply'];?>"></div><? } ?>
</td></tr></table>
</form>
</div>
<? } ?>
